package com.apes.scm.settle.settle.model;

import com.alibaba.fastjson.annotation.JSONField;
import com.apes.framework.jpa.BaseModel;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.plugin.soe.annotations.State;
import com.apes.framework.plugin.soe.annotations.States;
import com.apes.framework.plugin.soe.metadata.BusinessAttributeValue;
import com.apes.framework.util.ApesConst;
import com.apes.framework.util.DateUtil;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.util.Date;
import java.util.Objects;

@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = TaxBillItem.CONST_TABLE_NAME,indexes = {
        @Index(name = "idx_bill_id", columnList = "tax_bill_id")
})
public class TaxBillItem extends BaseModel {
    public static final String CONST_TABLE_NAME = "acc_tax_bill_item";

    @Id
    @Column(name = "id")
    @ApesGenerator(code = CONST_TABLE_NAME, name = "税票明细")
    private String id;

    @NotNull
    @ManyToOne(optional = false)
    @JoinColumn(name = "tax_bill_id", referencedColumnName = "id")
    private TaxBill taxBill; /*税票主表*/

    @States(name = "发票类型", values = {
            @State(id = "not", name = "无发票"),
            @State(id = "defalut", name = "普通发票"),
            @State(id = "valueAdded", name = "增值税发票"),
            @State(id = "red", name = "红票")})
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "type", nullable = false, referencedColumnName = "id")
    private BusinessAttributeValue type;


    @Column(name = "code", length = 64)
    private String code; //发票号码

    @Column(name = "amount", nullable = false, columnDefinition = ApesConst.FIELD_16_4_0)
    private double amount = 0; //发票金额

    @Column(name = "freeze_Qty", nullable = false, columnDefinition = ApesConst.FIELD_16_4_0)
    private double freezeAmount = 0; //冻结金额

    public void changeFreezeAmount(double incurred) {
        this.freezeAmount += incurred;
//        if (calcPendingQty() < 0) throw new RuntimeException("冻结金额加上已结算金额不能大于单据金额！");
    }

    @Column(name = "rebate_amount", nullable = false, columnDefinition = ApesConst.FIELD_16_4_0)
    private double rebateAmount = 0; //折扣金额

    @Column(name = "actual_amount", nullable = false, columnDefinition = ApesConst.FIELD_16_4_0)
    private double actualAmount = 0; //已付款金额

    public void changeActualAmount(double incurred) {
        this.actualAmount += incurred;
//        if (calcPendingQty() < 0) throw new RuntimeException("冻结金额加上已结算金额不能大于单据金额！");
    }

    @Column(name = "tax_amount", nullable = false, columnDefinition = ApesConst.FIELD_16_4_0)
    private double taxAmount = 0; //税金

    @Column(name = "tax_loss", nullable = false, columnDefinition = ApesConst.FIELD_16_4_0)
    private double taxLoss = 0; //税金损失

    @Column(name = "expense_amount", nullable = false, columnDefinition = ApesConst.FIELD_16_4_0)
    private double expenseAmount = 0; //费用

    @JSONField(format = DateUtil.SHOW_DATE_FORMAT)
    @Column(name = "tax_return_date", nullable = false)
    private Date taxReturnDate; //税票日期

    @Column(name = "note", length = 250)
    private String note; //摘要

    @Column(name = "settle_id", length = 250)
    private String settle_id; //结算单号


    @Transient
    private String settleId;

    public String getSettleId() {
        if (taxBill.getSettle() == null) return null;
        return Objects.isNull(taxBill.getSettle().getOrigin()) ? taxBill.getSettle().getId() : taxBill.getSettle().getOrigin();
    }

    /**
     * 来源单据类型取结算单源单据
     */
    @Transient
    private String originBusinessType;

    public String getOriginBusinessType() {
        if (taxBill.getSettle() == null) return null;
        return taxBill.getSettle().getOriginBusinessName();
    }


    @Transient
    @JSONField(format = DateUtil.SHOW_DATE_FORMAT)
    private Date taxBillApproveDate;

    public Date getTaxBillApproveDate() {
        if (taxBill==null) return null;
        return taxBill.getApproveDate();
    }
    /**
     * 计算剩余金额
     */
    public double calcPendingQty() {
        return amount - freezeAmount - actualAmount;
    }

    @Transient
    private String paymentMonth;

    public String getPaymentMonth() {
        if (taxBill == null) return null;
        return taxBill.getPaymentMonth();
    }
}
